<?php
class cartoon_util_interface{
	
	
	/**
	 * 获取分类列表
	 *	@param boolean $flush
	 *  @return  array
	 */
	public static function cacheGetCateList($flush = false){
		$cacheKey = array(
			'path' =>	'category/',
			'filename'	=>	'category',
			'expire'	=>	3600*24*7
		);
		
		return  Model::factory('cartoon_util_cache')->getCacheData($cacheKey,array(array(__CLASS__,getCateList),array()),$flush);
	}
	
	/**
	 * 获取缓存列表
	 *
	 * @param unknown_type $flush
	 * @return unknown
	 */
	public static function getCateList($order=null){
		$return = array();
		if($rs = Model::factory('cartoon_model_data')->getCateList($order)){
			while ($row = current($rs)){
				$return[$row['id']] = $row;
				next($rs);
			}
		}
		return $return;
	}

    /**
     * @static
     * @param  $cateid
     * @param  $flush
     * @return 缓存获取分类下最新的$num个漫画
     */
    public static  function cacheGetListByCateId($cateid,$num=5,$flush=false){
        $cacheKey = array(
			'path' =>	'cartoon/'.$cateid.'/',
			'filename'	=>	'cartoon_'.$cateid,
			'expire'	=>	3600*24
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cacheKey,array(array(__CLASS__,getListByCateId),array($cateid,$num)),$flush);
    }

    /**
	 * 获取缓存列表
	 *
	 * @param unknown_type $flush
	 * @return unknown
	 */
	public static function getListByCateId($cateid){
		return Model::factory('cartoon_model_data')->getCateList();
	}
	
	/**
     * @static
     * @param  $cateid
     * @param  $flush
     * @return 缓存获取漫画的下前$num个漫画
     */
    public static  function cacheGetChapter($cartoon_id,$num=5,$flush=false){
        $cacheKey = array(
			'path' =>	'cartoon/newchapter/',
			'filename'	=>	'cartoon_'.$cartoon_id,
			'expire'	=>	3600*2
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cacheKey,array(array(__CLASS__,getChapter),array($cartoon_id,$num)),$flush);
    }
    
    
	public static function getChapter($cartoon_id,$num = 5){
		return Model::factory('cartoon_model_chapter')->getChapterList($cartoon_id,$num);
	}
	
	/**
	 * 获取作者列表
	 *
	 * @return array
	 */
	public static function getAuthors(){
		$return  = false;
		if($rs = Model::factory('cartoon_model_data')->getAuthorList()){
			reset($rs);
			while ($row = current($rs)){
				$return[$row['id']] = $row;
				next($rs);
			}
		}
		return $return;
	}
	
	/**
	 * 获取漫画作者列表
	 *	@param boolean $flush
	 *  @return  array
	 */
	public static function cacheGetAuthors($flush = false){
		$cacheKey = array(
			'path' =>	'authors/',
			'filename'	=>	'authors',
			'expire'	=>	3600*24*7
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cacheKey,array(array(__CLASS__,getAuthors),array()),$flush);
	}
	
	/**
	 * 缓存获取tag列表，列表，按分类列出
	 *
	 * @param unknown_type $flush
	 * @return unknown
	 */
	public static  function cacheGetTags($type = NULL ,$flush = false){
		$cachekey = array(
				'path'=>'tags/',
				'filename' 	=>	'tags',
				'expire'	=>	3600*12		//缓存过期时间
		);
		$rs = Model::factory('cartoon_util_cache')->getCacheData($cachekey,array(array(__CLASS__,getTags),array()),$flush);
		if(in_array($type,array_keys($_SERVER['cartoon']['tag_type']))){
			return $rs[$type];
		}
		
		return $rs;
	}
	
	/**
	 * 获取首页推荐的轮播图片
	 *
	 * @param unknown_type $num
	 */
	public static function cacheGetRoll($flush = false){
		$cachekey = array(
				'path'=>'roll/',
				'filename' 	=>	'roll',
				'expire'	=>	0		//缓存过期时间
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cachekey,array(array(__CLASS__,getRoll),array()),$flush);
	}
	/**
	 * 获取首页轮播列表
	 *
	 * @param unknown_type $num
	 */
	public static function getRoll(){
		return Model::factory('cartoon_model_roll')->getListNum(4);
	}
	
	/**
	 * 获取首页推荐的轮播图片
	 *
	 * @param unknown_type $num
	 */
	public static function cacheGetAdv($type= null,$flush = false){
		$cachekey = array(
				'path'=>'adv/'.($type?$type.'/':''),
				'filename' 	=>	$type?'adv_'.$type:'adv',
				'expire'	=>	0		//缓存过期时间
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cachekey,array(array(__CLASS__,getAdv),array($type)),$flush);
	}
	
	/**
	 * 获取首页轮播列表
	 *
	 * @param unknown_type $num
	 */
	public static function getAdv($type){
		return 	Model::factory('cartoon_model_adv')->getListNum($type);
	}
	
	/**
	 * 获取首页推荐的轮播图片
	 *
	 * @param unknown_type $num
	 */
	public static function cacheGetUpLog($flush = false){
		$cachekey = array(
				'path'=>'uplog/',
				'filename' 	=>	'uplog',
				'expire'	=>	0		//缓存过期时间
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cachekey,array(array(__CLASS__,getUpLog),array()),$flush);
	}
	
	/**
	 * 获取首页轮播列表
	 *
	 * @param unknown_type $num
	 */
	public static function getUpLog(){
		return 	Model::factory('cartoon_model_data')->getUpList();
	}
	
	/**
	 * 最近更新
	 *
	 * @param unknown_type $flush
	 * @return unknown
	 */
	public static function cacheGetLog($flush = false){
		$cachekey = array(
				'path'=>'uplogs/',
				'filename' 	=>	'uplog',
				'expire'	=>	0		//缓存过期时间
		);
		return  Model::factory('cartoon_util_cache')->getCacheData($cachekey,array(array(__CLASS__,getLog),array()),$flush);
	}
	
	public static function getLog(){
		return Model::factory('cartoon_model_data')->getUpList();
	}
	
	/**
	 * 获取推荐的漫画列表
	 *
	 * @param unknown_type $type
	 * @param unknown_type $flush
	 */
	public static function cacheGetRecomCT($type,$flush = false){
		$_SERVER['cache'][$type]['filename'] .=	$type;
		return Model::factory('cartoon_util_cache')->getCacheData($_SERVER['cache'][$type],array(array(__class__,'getRecomCTList'),array($type,$_SERVER['cache'][$type]['num'],$_SERVER['cache'][$type]['order'])),$flush);
	}
	
	
	/**
	 * 获取推荐漫画列表
	 *
	 * @param unknown_type $type
	 * @param unknown_type $num
	 * @param unknown_type $order
	 * @return unknown
	 */
	public static function getRecomCTList($type = CONST_CT::RECOM_HOME ,$num=8,$order = ''){
		return Model::factory('cartoon_model_data')->getCartoonListRecom($type,$num,$order);
	}
	
	/**
	 * 获取推荐的漫画列表
	 *
	 * @param unknown_type $type
	 * @param unknown_type $flush
	 */
	public static function cacheCtNewList($num = 4,$flush = false){
		return Model::factory('cartoon_util_cache')->getCacheData($_SERVER['cache'][CONST_CT::RECOM_NEW],array(array(__class__,'getCtNewList'),array($_SERVER['cache'][CONST_CT::RECOM_NEW]['num'],$_SERVER['cache'][CONST_CT::RECOM_NEW]['order'])),$flush);
	}
	
	
	/**
	 * 获取推荐漫画列表
	 *
	 * @param unknown_type $type
	 * @param unknown_type $num
	 * @param unknown_type $order
	 * @return unknown
	 */
	public static function getCtNewList($num=4,$order = ''){
		return Model::factory('cartoon_model_data')->getCartoonListNew($num,$order);
	}
	
	/**
	 * 查询所有的tag列表
	 *
	 * @return unknown
	 */
	public static  function getTags(){
		return Model::factory('cartoon_model_data')->getTagByType();		
	}
	
	/**
	 * 返回错误信息
	 * @param unknown_type $error
	 */
	public static function exit_err($error){
		header("HTTP/1.1 500 $error");
		exit(0);
	}
	
	
	/**
	 * 获取中文汉字的首字母
	 *
	 * @param  string  $str	要转换的汉字
	 * @return unknown
	 */
	function getfirstchar($str){
		$firstchar_ord=ord(strtoupper($str{0}));
		if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $str{0};
		$s=iconv("UTF-8","gb2312", $str);
		$asc=ord($s{0})*256+ord($s{1})-65536;
		if($asc>=-20319 and $asc<=-20284)return "A";
		if($asc>=-20283 and $asc<=-19776)return "B";
		if($asc>=-19775 and $asc<=-19219)return "C";
		if($asc>=-19218 and $asc<=-18711)return "D";
		if($asc>=-18710 and $asc<=-18527)return "E";
		if($asc>=-18526 and $asc<=-18240)return "F";
		if($asc>=-18239 and $asc<=-17923)return "G";
		if($asc>=-17922 and $asc<=-17418)return "H";
		if($asc>=-17417 and $asc<=-16475)return "J";
		if($asc>=-16474 and $asc<=-16213)return "K";
		if($asc>=-16212 and $asc<=-15641)return "L";
		if($asc>=-15640 and $asc<=-15166)return "M";
		if($asc>=-15165 and $asc<=-14923)return "N";
		if($asc>=-14922 and $asc<=-14915)return "O";
		if($asc>=-14914 and $asc<=-14631)return "P";
		if($asc>=-14630 and $asc<=-14150)return "Q";
		if($asc>=-14149 and $asc<=-14091)return "R";
		if($asc>=-14090 and $asc<=-13319)return "S";
		if($asc>=-13318 and $asc<=-12839)return "T";
		if($asc>=-12838 and $asc<=-12557)return "W";
		if($asc>=-12556 and $asc<=-11848)return "X";
		if($asc>=-11847 and $asc<=-11056)return "Y";
		if($asc>=-11055 and $asc<=-10247)return "Z";
		return null;
	}
}
?>